package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsFootprintMapper;
import com.shop.pojo.DtsFootprint;
import com.shop.pojo.DtsUser;
import com.shop.service.DtsFootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DtsFootprintServiceImpl
 * @Author xiao宋
 * @Date 18:32  2022/10/16
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsFootprintServiceImpl implements DtsFootprintService {
    @Autowired
    DtsFootprintMapper footprintMapper;
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, Integer userId, Integer goodsId) {
        // 正好有个 DtsFootprint 实体类是对应的
        IPage<DtsFootprint> iPage = new Page<>(page, limit);
        QueryWrapper<DtsFootprint> w = new QueryWrapper<>();
//        前端显示的用户名为 nickname。  并不是username
        if (userId!=null) {
            w.eq("user_id", userId);
//            w.eq(DtsFootprint::getUserId, userId);
        }
        if (goodsId!=null) {
            w.eq("goods_id", goodsId);
        }
//        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }

        footprintMapper.selectPage(iPage, w);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());

        return data;
    }
}
